package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageAll;
import com.itheima.pojo.entity.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 班级管理Controller
 * @author 友人A
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result findClazzs(@RequestParam(required = false) String name,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize,
                             @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
                             @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end) {
        PageAll clazzList = clazzService.findClazzs(name, page, pageSize,begin,end);
        log.info("传入分页条件,name:{},page:{},pageSize:{},begin:{},end:{}",name,page,pageSize,begin,end);
        return Result.success(clazzList);
    }

    @GetMapping("/list")
    public Result findClazzList() {
        List<Clazz> clazzList = clazzService.findClazzList();
        return Result.success(clazzList);
    }

    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id) {
        clazzService.deleteClazz(id);
        log.info("传入班级id：{}",id);
        return Result.success();
    }

    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz) {
        clazzService.addClazz(clazz);
        log.info("传入班级对象：{}",clazz);
        return Result.success();
    }

    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        log.info("传入班级对象：{}",clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getClazzById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getClazzById(id);
        log.info("传入班级id：{}",id);
        return Result.success(clazz);
    }

}
